import datetime
import logging
import re

import pytest

from resource.pom.settings import Settings
from utils.manager import MatchRules, Direction


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，点击设置-系统-日期和时间-时间')
        today = datetime.datetime.now()
        device.hdc_shell("date '{}'".format(today.strftime("%Y-%m-%d") + ' 21:34:00'))
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.dirc_fling(Direction.Down)
        device.click(*self.app.system_item)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('日期和时间'))
        logging.info('预期结果1.可正常进入日期界面')
        device.refresh_layout()
        device.assert_text_exist('24小时制')
        device.assert_type_exist('Toggle')
        device.assert_text_exist('日期')
        device.assert_text_exist('{}年{}月{}日'.format(today.year, today.month, today.day))
        device.assert_text_exist('时间')

        logging.info('步骤2.开启/关闭24小时制')
        switch = device.get_element_by_type('Toggle')
        # 先关闭24小时制获取当前时间
        if switch.get('checked') == 'true':
            device.click_element(switch)
            device.refresh_layout()
        current_time = device.get_element_by_text('午', rule=MatchRules.Contain).get('text')
        match_rst = re.search('(?P<hour>\d+):(?P<min>\d+)', current_time)
        current_hour = match_rst.group('hour')
        current_min = match_rst.group('min')
        logging.info('预期结果2.1 打开24小时制，时间显示为上午12制和下午12制，状态栏时间显示正确的12小时时间')
        device.click_element(switch)
        statusbar_winid = device.get_statusbar_winid()
        device.refresh_layout()
        if current_time.startswith('上午'):
            statusbar24_t1 = device.get_element_by_condition(condition={'hostWindowId': statusbar_winid, 'text': '{:0>2}:{:0>2}'.format(current_hour, int(current_min))})
            statusbar24_t2 = device.get_element_by_condition(condition={'hostWindowId': statusbar_winid, 'text': '{:0>2}:{:0>2}'.format(current_hour, int(current_min) + 1)})
            assert statusbar24_t1 or statusbar24_t2, '状态栏时间显示不正确'
        else:
            statusbar24_t1 = device.get_element_by_condition(condition={'hostWindowId': statusbar_winid, 'text': '{:0>2}:{:0>2}'.format(int(current_hour) + 12, int(current_min))})
            statusbar24_t2 = device.get_element_by_condition(condition={'hostWindowId': statusbar_winid, 'text': '{:0>2}:{:0>2}'.format(int(current_hour) + 12, int(current_min) + 1)})
            assert statusbar24_t1 or statusbar24_t2, '状态栏时间显示不正确'

        logging.info('预期结果2.2 关闭24小时制，时间显示为24制，状态栏时间显示正确的12小时制时间')
        device.click_element(switch)
        device.refresh_layout()
        statusbar12_t1 = device.get_element_by_condition(condition={'hostWindowId': statusbar_winid, 'text': '{:0>2}:{:0>2}'.format(current_hour, int(current_min))})
        statusbar12_t2 = device.get_element_by_condition(condition={'hostWindowId': statusbar_winid, 'text': '{:0>2}:{:0>2}'.format(current_hour, int(current_min) + 1)})
        assert statusbar12_t1 or statusbar12_t2, '状态栏时间显示不正确'

        logging.info('步骤3.上下滑动修改"上午、下午、时、分"后点击确定')
        time_item = device.get_element_by_text('时间')
        device.click_element(time_item)
        device.refresh_layout()
        device.swipe(160, 935, 160, 1035)
        device.swipe(365, 935, 365, 1035)
        device.swipe(570, 935, 570, 1035)
        device.wait(1)
        confirm_btn = device.get_element_by_text('确定')
        device.click_element(confirm_btn)
        logging.info('预期结果3.0时间修改生效，通知栏时间显示一致')
        device.refresh_layout()
        new_time = device.get_element_by_text('午', rule=MatchRules.Contain).get('text')

        match_rst = re.search('(?P<hour>\d+):(?P<min>\d+)', new_time)
        new_hour = match_rst.group('hour')
        new_min = match_rst.group('min')
        if new_time.startswith('上午'):
            real_time1 = '{:0>2}:{:0>2}'.format(new_hour, int(new_min))
            real_time2 = '{:0>2}:{:0>2}'.format(new_hour, int(new_min) + 1)
        else:
            real_time1 = '{:0>2}:{:0>2}'.format(int(new_hour) + 12, int(new_min))
            real_time2 = '{:0>2}:{:0>2}'.format(int(new_hour) + 12, int(new_min) + 1)
        logging.info('预期结果3.1检查状态栏时间修改是否生效')
        new_statusbar_t1 = device.get_element_by_condition(condition={'hostWindowId': statusbar_winid, 'text': real_time1})
        new_statusbar_t2 = device.get_element_by_condition(condition={'hostWindowId': statusbar_winid, 'text': real_time2})
        assert new_statusbar_t1 or new_statusbar_t2, '状态栏时间未更新'
        # 左边补0
        device.dropdown_notification_bar()
        device.refresh_layout()
        device.swipe_up_dropdown_pan()
        rt1 = device.get_element_by_text(real_time1)
        rt2 = device.get_element_by_text(real_time2)
        assert rt1 or rt2, '通知栏时间显示不正确'
